<!-- 引入编辑器的插件 -->
<link rel="stylesheet" href="/static/kindeditor/themes/default/default.css">
<script src="/static/kindeditor/kindeditor-all-min.js"></script>
<script src="/static/kindeditor/lang/zh-CN.js"></script>

<div class="layui-container">
  <div class="layui-row layui-col-space15">
    <div class="layui-col-md8 content detail">
      <div class="fly-panel detail-box">
        <h1>{$post.title}</h1>
        <div class="fly-detail-info">
          <!-- 状态 -->
          {if($post.state eq '1')}
            <span class="layui-badge layui-bg-black">置顶</span>
          {elseif($post.state eq '2')}
            <span class="layui-badge layui-bg-red">精帖</span>
          {elseif($post.state eq '3')}
            <span class="layui-badge layui-bg-green fly-detail-column">热门</span>
          {/if}

          <!-- 采纳状态 -->
          {if($post.accept)}
            <span class="layui-badge" style="background-color: #5FB878;">已结</span>
          {else /}
            <span class="layui-badge" style="background-color: #999;">未结</span>
          {/if}
          
          <div class="fly-admin-box" data-id="123">
            {if($LoginUser)}
              {if($LoginUser.id == $post.userid)}
              <span class="layui-btn layui-btn-xs jie-admin" data-id="{$post.id}" id="PostDel" type="del">删除</span>
              {/if}
            {/if}
            {if($LoginUser && $LoginUser.id != $post.userid)}
              {if($CollectStatus)}
                <span class="layui-btn layui-btn-xs jie-admin layui-bg-red"  data-postid="{$post.id}" type="edit" id="CollectDel">
                  <a href="javascript:void(0)" style="color: #fff;">取消收藏</a>
                </span>
              {else /}
                <span class="layui-btn layui-btn-xs jie-admin layui-bg-blue" data-postid="{$post.id}" type="edit" id="CollectAdd">
                  <a href="javascript:void(0)" style="color: #fff;">收藏</a>
                </span>
              {/if}
            {/if}
            <!-- <span class="layui-btn layui-btn-xs jie-admin" type="set" field="stick" rank="1">置顶</span>  -->
            <!-- <span class="layui-btn layui-btn-xs jie-admin" type="set" field="stick" rank="0" style="background-color:#ccc;">取消置顶</span> -->
            
            <!-- <span class="layui-btn layui-btn-xs jie-admin" type="set" field="status" rank="1">加精</span>  -->
            <!-- <span class="layui-btn layui-btn-xs jie-admin" type="set" field="status" rank="0" style="background-color:#ccc;">取消加精</span> -->
          </div>
          <span class="fly-list-nums"> 
            <a href="#comment"><i class="iconfont" title="回答">&#xe60c;</i> {$post.comment_total}</a>
            <i class="iconfont" title="人气">&#xe60b;</i> {$post.visit_total}
          </span>
        </div>
        <div class="detail-about">
          <a class="fly-avatar" href="{:url('home/user/index',['userid' => $post.userid])}">
            <img src="{$post.user.avatar ? $post.user.avatar : '/static/home/res/images/avatar/default.png'}" alt="贤心">
          </a>
          <div class="fly-detail-user">
            <a href="{:url('home/user/index',['userid' => $post.userid])}" class="fly-link">
              <cite>{$post.user.nickname}</cite>
              {if($post.user.auth)}
                <i class="iconfont icon-renzheng" title="认证信息：{$post.user.nickname}"></i>
              {/if}
              {if($post.user.vip > 0)}
                <i class="layui-badge fly-badge-vip">VIP {$post.user.vip}</i>
              {/if}
            </a>
            <span>{$post.createtime | date="Y-m-d H:i"}</span>
          </div>
          <div class="detail-hits" id="LAY_jieAdmin" data-id="123">
            <span style="padding-right: 10px; color: #FF7200">悬赏：{$post.point} 积分</span>
            {if($LoginUser)}
              {if($LoginUser.id == $post.userid && !$post['accept'])}
                <span class="layui-btn layui-btn-xs jie-admin" type="edit"><a href="{:url('home/post/edit',['postid' => $post.id])}">编辑此贴</a></span>
              {/if}
            {/if}
          </div>
        </div>
        <div class="detail-body photos">
          {$post.content|raw}
        </div>
      </div>

      <div class="fly-panel detail-box" id="flyReply">
        <fieldset class="layui-elem-field layui-field-title" style="text-align: center;">
          <legend>回帖</legend>
        </fieldset>

        <!-- 原始结构 -->
        <ul class="jieda" id="jieda">

          {if($CommentList)}
            {:CommentHtml($CommentList,$post,$LoginUser)}
          {else /}
            <!-- 无数据时 -->
            <li class="fly-none">消灭零回复</li>
          {/if}
        </ul>
        
        <!-- 如果没有采纳人 就显示评论 未结 -->
        {if($LoginUser && !$post.accept && $LoginUser.auth > 0)}
        <div id="comment" class="layui-form layui-form-pane">
          <form action="{:url('home/comment/add')}" method="post">
            <div class="layui-form-item">
              <!-- 回复昵称 -->
              <h2 id="reply-nickname"></h2>
            </div>

            <div class="layui-form-item layui-form-text">
              <div class="layui-input-block">
                <!-- 富文本编辑器 -->
                <textarea id="content" name="content" placeholder="请输入内容" class="layui-textarea" style="height: 300px;"></textarea>
              </div>
            </div>
            
            <div class="layui-form-item">
              <!-- 这条评论的上一级 -->
              <input type="hidden" name="pid" value="0" />
              <!-- 帖子id -->
              <input type="hidden" name="postid" value="{$post.id}" />
              <button type="submit" class="layui-btn">提交回复</button>
            </div>
          </form>
        </div>
        {/if}
      </div>
    </div>
    <div class="layui-col-md4">
      <dl class="fly-panel fly-list-one">
        <dt class="fly-panel-title">本周热议</dt>
        {if($PostWeek)}
          {foreach($PostWeek as $item)}
            <dd>
              <a href="{:url('home/index/info',['postid' => $item.post.id])}">{$item.post.title}</a>
              <span><i class="iconfont icon-pinglun1"></i> {$item.post.comment_total}</span>
            </dd>
          {/foreach}
        {else /}
          <div class="fly-none">没有热议</div>
        {/if}

        <!-- 无数据时 -->
        <!--
        
        -->
      </dl>

      <div class="fly-panel" style="padding: 20px 0; text-align: center;">
        <img src="/static/home/res/images/weixin.jpg" style="max-width: 100%;" alt="layui">
        <p style="position: relative; color: #666;">微信扫码关注 layui 公众号</p>
      </div>

    </div>
  </div>
</div>
<script>
  var editor;
  KindEditor.ready(function(K){
    editor = K.create('textarea[name="content"]', {
      allowFileManager : true
    });
  })

  layui.use(['layer'],function(){
    var $ = layui.jquery,
    layer = layui.layer

    $('#PostDel').click(function(){
      // 当前对象变成全局
      var that = $(this);
      layer.confirm('确认删除该帖子？',{icon: 3, title:'删除'},function(index){
        // console.log();
        var id = that.data('id');
        $.ajax({
          type: "post",
          url: `{:url('home/post/del')}`,
          data: {
            id:id
          },
          dataType: "json",
          success: function (response) {
            // console.log(response);
            if(response.code == 1)
            {
              layer.msg(response.msg,{icon:1},function(){
                location.href = `{:url('home/index/index')}`;
                return false;
              })
            }else{
              layer.msg(response.msg,{icon:2})
            }
          }
        });

        // 关闭窗口
        layer.close(index);
      })
        
    });

    // 添加收藏
    $("#CollectAdd").on('click', function(){
      let that = $(this)
      layer.confirm('确认收藏该帖子？',{icon:3,title:'收藏'},function(index){
        let id = that.data('postid')
        // 发送异步请求
        $.ajax({
          url:`{:url('home/collection/add')}`,
          type:'post',
          data:{
            postid:id
          },
          dataType:'json',
          success:function(success)
          {
            if(success.code === 1)
            {
              // 成功
              layer.msg(success.msg, {
                icon: 1,
                time: 2000 // 2秒关闭（如果不配置，默认是3秒）
              }, function(){
                // 直接刷新界面
                location.reload()
              })
            }else
            {
              // 失败
              layer.msg(success.msg)
            }
          }
        })

        // 关闭窗口
        layer.close(index);

      })
        
    })

    $("#CollectDel").on('click', function(){
      let that = $(this)
      layer.confirm('确认取消收藏该帖子？',{icon:3,title:'取消收藏'},function(index){
        let id = that.data('postid')
        // 发送异步请求
        $.ajax({
          url:`{:url('home/collection/add')}`,
          type:'post',
          data:{
            postid:id
          },
          dataType:'json',
          success:function(success)
          {
            if(success.code === 1)
            {
              // 成功
              layer.msg(success.msg, {
                icon: 1,
                time: 2000 // 2秒关闭（如果不配置，默认是3秒）
              }, function(){
                // 直接刷新界面
                location.reload()
              })
            }else
            {
              // 失败
              layer.msg(success.msg)
            }
          }
        })

        // 关闭窗口
        layer.close(index);

      })
        
    })

    $('.com-del').click(function(){
      // 当前对象变成全局
      var that = $(this);
      layer.confirm('确认删除该评论？',{icon: 3, title:'删除'},function(index){
        // console.log();
        var id = that.data('comid');
        $.ajax({
          type: "post",
          url: `{:url('home/comment/del')}`,
          data: {
            comid:id
          },
          dataType: "json",
          success: function (response) {
            // console.log(response);
            if(response.code == 1)
            {
              layer.msg(response.msg,{icon:1},function(){
                  location.reload();
                  return false;
              })
            }else{
                  layer.msg(response.msg,{icon:2})
                  return false;
            }
          }
        });

        // 关闭窗口
        layer.close(index);
      })
        
    });

    // 回复
    $('.reply').on('click',function(){

      // 获取回复的昵称
      var nickname = $(this).data('nickname');
      $('#reply-nickname').html('回复@'+ nickname);
      var pid = $(this).data('pid');

      $('input[name="pid"]').val(pid);

      // 富文本编辑器产生光标
      editor.focus();
    })


    // 点赞
    $('.jieda-zan').click(function(){
      var comid = $(this).data('id');

      // 判断是否有这个类名 如果有就移除，没有就添加
      // $('.jieda-zan').toggleClass('zanok');
      if($(this).hasClass('zanok'))
      {
        var num = Number($(this).find('em').text()) - 1;
        $(this).find('em').text(num);
        $(this).removeClass('zanok');
      }else{
        var num = Number($(this).find('em').text()) + 1;
        $(this).find('em').text(num);
        $(this).addClass('zanok');
      }

      $.ajax({
        type:'post',
        url:`{:url('home/comment/like')}`,
        data:{
          comid:comid
        },
        dataType:"json",
        success:function(res)
        {
          if(res.code == 1)
          {
            layer.msg(res.msg,{icon:6});
          }else{
            layer.msg(res.msg,{icon:2});
          }
        }
      })
    });

    // 采纳
    $('.accept').click(function(){
      // var comid = $(this).data('comid');
      var that = $(this);
      layer.confirm('是否采纳该条评论？',{icon: 3, title:'采纳'},function(index){
        var comid = that.data('comid');
        // 异步请求
        $.ajax({
          type:'post',
          url:`{:url('home/comment/accept')}`,
          data:{
            comid:comid
          },
          dataType:'json',
          success:function(res){
            if(res.code == 1)
              {
                layer.msg(res.msg,{icon:1},function(){
                  location.reload();
                  return false;
                })
              }else{
                layer.msg(res.msg,{icon:2})
              }
          }
        })

        // 关闭窗口
        layer.close(index);
      });
      
    })

  })
</script>